package com.wing.sys.dao;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.wing.common.CommonSpringJDBC;
import com.wing.common.util.PageBean;
import com.wing.sys.domain.Role;

@Repository
public class RoleDAO extends CommonSpringJDBC<Role,String> {

	@SuppressWarnings("all") 
	public Role findById(String id) {
		String sql = "select * from sys_role t where id = ? and (valid_flag is null or valid_flag != -1) ";
		return super.getSimpleJdbcTemplate().queryForObject(sql,
				ParameterizedBeanPropertyRowMapper.newInstance(Role.class), id);
	}

	public List<Map<String, Object>> findAll() {
		String sql = "select * from sys_role where (valid_flag is null or valid_flag != -1) ";
		return super.getSimpleJdbcTemplate().queryForList(sql);
	}

	public PageBean findPageList(int currentPage, int pageSize, String role_type_id,String siteId) {
		String where= " r.site_id='"+siteId+"' and r.role_type_id=c.code and c.id='"+role_type_id+"' and (r.valid_flag is null or r.valid_flag != -1) ";
		String sql1=" SELECT r.* FROM sys_role r,sys_code c where "+where+" ORDER BY r.id ASC " +
				" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		String sql2 = "select count(r.id) from sys_role r,sys_code c where"+where;
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}


	public void delete(String id) {
		String sql = "update sys_role set valid_flag = -1 where id=?";
		super.getSimpleJdbcTemplate().update(sql, id);
	}
	
	/**
	 * 获取角色下的功能模块
	 * @param currentPage
	 * @param pageSize
	 * @param roleId
	 * @return
	 */
	public PageBean findPageModulesByRoleId(int currentPage, int pageSize,
			String roleId) {
		String where= " rm.moduleid=m.id and rm.roleid='"+roleId+"' ";
		String sql1=" SELECT m.* FROM sys_role_module rm,sys_module m where "+where+" ORDER BY m.order_num ASC, m.id ASC  " +
			" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		String sql2 = "select count(m.id) from sys_role_module rm,sys_module m where"+where;
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}

	/**
	 * 获取角色下的用户
	 * @param currentPage
	 * @param pageSize
	 * @param roleId
	 * @return
	 */
	public PageBean findPageUsersByRoleId(int currentPage, int pageSize,
			String roleId) {
		String where= " ru.userid=u.id and ru.roleid='"+roleId+"' and (u.valid_flag is null or u.valid_flag != '-1') ";
		String sql1=" select u.* FROM sys_role_user ru,sys_user u where "+where+" ORDER BY u.order_num ASC ,u.id ASC  " +
			" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		String sql2 = "select count(u.id) from sys_role_user ru,sys_user u where"+where;
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}

	/**
	 * 获取角色下的组织机构
	 * @param currentPage
	 * @param pageSize
	 * @param roleId
	 * @return
	 */
	public PageBean findPageOrgsByRoleId(int currentPage, int pageSize,
			String roleId) {
		String where= " ro.orgid=o.id and ro.roleid='"+roleId+"' and (o.valid_flag is null or o.valid_flag != '-1') ";
		String sql1=" SELECT o.* FROM sys_role_org ro,sys_organization o where "+where+" ORDER BY o.order_num ASC ,o.id ASC " +
			" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		String sql2 = "select count(o.id) from sys_role_org ro,sys_organization o where"+where;
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}
}
